package swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import model.ListDialogLogic;
import tableDancer.Revierinfo;
import datenbank.DatabaseQueryMachine;

@SuppressWarnings("serial")
public class ListDialog
extends JDialog
implements SwingInterface
{
	public JList li_auswahl;
	private JButton bt_load, bt_abort;
	public LinkedList<Revierinfo> ll_revinf;
	public MainFrame mf_main;
	private ListDialogLogic ldl;
	private JPanel p_south;

	public ListDialog(MainFrame mf_main)
	{
		this.mf_main = mf_main;
		setTitle("Auswahl");
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		setInitiate();
		setLayout();
		setListener();
	}

	public void setInitiate()
	{
		ldl = new ListDialogLogic(this);
		ll_revinf = new DatabaseQueryMachine().getAllRevierinfo();

		DefaultListModel dlm = new DefaultListModel();
		for(Revierinfo ri : ll_revinf)
		{
			dlm.addElement(ri);
		}
		li_auswahl = new JList(dlm);
		li_auswahl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		bt_load = new JButton("Laden");
		bt_abort = new JButton("Abbrechen");

		p_south = new JPanel();
	}

	public void setLayout()
	{
		//Allgemeiner Contentlayer
		setLayout(new BorderLayout());
		add(new JScrollPane(li_auswahl), BorderLayout.CENTER);
		add(p_south, BorderLayout.SOUTH);

		//South
		p_south.setLayout(new FlowLayout());
		p_south.add(bt_load);
		p_south.add(bt_abort);
	}

	public void setListener()
	{
		bt_load.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ldl.loadRevier();
			}
		});

		bt_abort.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ldl.closeDialog();		
			}
		});
	}

	public void start()
	{
		setLocation(320,320);
		setSize(200,200);
		setResizable(false);
		setVisible(true);
	}
}
